## AmiClient
<strong><em>Asterisk Manager Interface (AMI) client for .NET based around the Reactive Extensions (Rx) framework</em></strong>

AMI as explained in _Asterisk: the Definitive Guide_:<br />
http://www.asteriskdocs.org/en/3rd_Edition/asterisk-book-html-chunk/asterisk-AMI.html

AMI command reference:<br />
https://wiki.asterisk.org/wiki/display/AST/AMI+Actions

.NET Reactive Extensions (Rx) examples:<br />
http://rxwiki.wikidot.com/101samples

<hr />

### Download

Pre-compiled, strongly named binaries are available for download on CodePlex.

<strong>[AmiClient-1.0.77.zip](https://amiclient.codeplex.com/releases/view/113552)</strong>

**Note:** If you are using a variant of .NET 4, this library depends on the [Async for .NET Framework 4 NuGet package](https://nuget.org/packages/Microsoft.Bcl.Async), which is included in the binary release.

<hr />

### Quick Start
```csharp
using System;
using System.Net.Sockets;

using Ami;
using Ami.SessionManager;

internal static class Demonstration
{
	private static async void Demonstrate()
	{
		// The AmiClient class is an IObservable wrapper around an arbitrary Stream
		// object. It consumes and parses incoming AMI messages and then publishes
		// them to interested Rx observers. Additionally, it provides a C# 5 async-
		// capable API for issuing Action requests and interpreting their results.

		using( var socket = new TcpClient( hostname: "localhost", port: 5038 ) )
		using( var client = new AmiClient( socket.GetStream() ) )
		{
			// AmiSessionManager is a utility class that (a) handles authentication
			// against an AMI server and (b) serves as an example of how to interact
			// with an AmiClient using the async/await pattern.

			var sessionManager = new AmiSessionManager( client );

			if( await sessionManager.Login( username: "admin", password: "amp111" ) == false )
			{
				return; // login failed
			}

			// As a demonstration, check to see that the server's SIP module is loaded
			// using the await keyword to determine success

			var r = await client.Publish(
				new AmiActionMessage( "ModuleCheck" )
				{
					{ "Module", "chan_sip" },
				} );

			if( r.Response.Equals( "Success" ) )
			{
				Console.WriteLine( "Module 'chan_sip' is loaded" );
			}
			else
			{
				Console.WriteLine( "Module 'chan_sip' is NOT loaded" );
			}

			sessionManager.Logoff();
		}
	}

	public static void Main( String[] args )
	{
		Demonstration.Demonstrate(); // async keyword can't be used on Main method
	}
}
```
